﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Besture.Common
{
    public class GenerateHtml
    {
        /// <summary>
        /// 生成页面
        /// </summary>
        /// <param name="templatePath">模版路径绝对路径</param>
        /// <param name="generatePath">生成路径绝对路径</param>
        /// <param name="replace">替换的名称和内容</param>
        /// <returns></returns>
        public  int Universal(string templatePath, string generatePath, Dictionary<string,string> replace)
        {
            try
            {
                //获取页面名称
                //string temName = templatePath.Substring(templatePath.LastIndexOf("\\") + 1,
                //                                        templatePath.LastIndexOf(".") -
                //                                        (templatePath.LastIndexOf("\\") + 1));
                
                string template =string.Empty; //模板内容
                using (StreamReader sr = new StreamReader(templatePath))
                {
                    template = sr.ReadToEnd(); //读取模板内容
                }
                if (!string.IsNullOrEmpty(template))
                {
                    /*模板内容替换*/
                    string html = template.Replace("[WebRootPath]", templatePath); //替换路径

                    foreach (KeyValuePair<string, string> keyValuePair in replace)
                    {
                        html = html.Replace(keyValuePair.Key, keyValuePair.Value);
                    }
                    using (StreamWriter sw = new StreamWriter(generatePath))
                    {
                        sw.Write(html); //写入静态化页面内容
                    }
                    return 1;
                }
                return 0;
            }
            catch (Exception e)
            {
                return 0;
            }
            finally
            {

            }
        }
    }
}
